﻿using System;
using System.Collections.Generic;
using System.Text;
using DevComponents.DotNetBar;
using System.Windows.Forms;
using AMS.UI.Controls.Search;

namespace AMS.UI.Controls
{
    public class SearchTextBox : ISearchTool
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="textbox"></param>
        public SearchTextBox(TextBoxItem textbox)
        {
            this.textboxItem = textbox;
            textbox.KeyDown += new KeyEventHandler(textbox_KeyDown);
            textbox.TextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(textbox_KeyDown);
            textbox.TextBox.TextChanged += new EventHandler(textbox_TextChanged);
            textbox.TextChanged += new EventHandler(textbox_TextChanged);
        }

        public SearchTextBox(TextBox txt)
        {
            this.textbox = txt;
            textbox.KeyDown += new KeyEventHandler(textbox_KeyDown);
            textbox.TextChanged += new EventHandler(textbox_TextChanged);
        }

        private TextBoxItem textboxItem;
        private TextBox textbox;
        public event EventHandler<KeywordChangedEventArgs> KeywordChanged;

        public string Keyword
        {
            get { return textboxItem == null? textbox.Text: textboxItem.Text; }
            set
            {
                if (textboxItem != null)
                {
                    textboxItem.Text = value;
                }
                else
                {
                    textbox.Text = value;
                }
            }
        }

        private string Text
        {
            get { return textbox == null ? textboxItem.Text : textbox.Text; }
        }

        protected virtual void OnKeywordChanged(KeywordChangedEventArgs e)
        {
            if (KeywordChanged != null)
            {
                KeywordChanged(this, e);
            }
        }

        private void textbox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && !e.Control)
            {
                OnKeywordChanged(new KeywordChangedEventArgs(Text, false));
                e.Handled = true;
                e.SuppressKeyPress = true;

                if (textbox != null)
                {
                    new App.DelayLoading(textbox.Parent, 50, new MethodInvoker(delegate()
                        {

                            textbox.Focus();
                        })).Execute();
                }
                else
                {
                    new App.DelayLoading(textboxItem.ContainerControl as Control, 50, new MethodInvoker(delegate()
                    {

                        textboxItem.Focus();
                    })).Execute();
                }
                   
            }
        }

        private void textbox_TextChanged(object sender, EventArgs e)
        {
            OnKeywordChanged(new KeywordChangedEventArgs(Text, true));
        }

        public void ShowTo(object attachingCtrl)
        {
            if (textbox == null)
            {
                textboxItem.Focus();
            }
            else
            {
                textbox.Focus();
            }
        }
    }
}
